<?
function reg(){
require_once('config.php');

//회원가입
	$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("시발 db연결이 안됨");
  if (isset($_POST['signup'])) {
    $username = mysqli_real_escape_string($dbc, trim($_POST['username']));
    $password1 = mysqli_real_escape_string($dbc, trim($_POST['password1']));
    $password2 = mysqli_real_escape_string($dbc, trim($_POST['password2']));

    if (!empty($username) && !empty($password1) && !empty($password2) && ($password1 == $password2)) {
      $query = "SELECT * FROM test_user WHERE username = '$username'";
      $data = mysqli_query($dbc, $query);
      if (mysqli_num_rows($data) == 0) {
        $query = "INSERT INTO test_user (username, password, join_date) VALUES ('$username', SHA('$password1'), NOW())";
        mysqli_query($dbc, $query);
		 echo '<p>회원 가입 완료<a href="index.php">로그인</a>.</p>';
        mysqli_close($dbc);
        exit();
      }
      else {
        echo '<p>이미 사용중니 다른 이름을 쓰세요.</p>';
        $username = "";
      }
    }
    else {
      echo '<p>기입 정보를 확인하세요.</p>';
    }
  }

  mysqli_close($dbc);
 
}

function reg_form(){
?><h1>회원가입</h1>
<h2>한글로 아이디 만들지마여</h2>
<form method="post" action="<? echo $_SERVER['PHP_SELF'];?>">
    ID:<input type="text" name="username" value="<? if(!empty($username)) echo $username; ?>" />
    PW:<input type="password" name="password1" />
    PW확인:<input type="password" name="password2" />
    <input type="submit" name="signup" value="가입" />
</form>

<br />

<?
}
function login(){
//로그인
  $error_msg = "";

  if (!isset($_SESSION['user_id'])) {
    if (isset($_POST['login'])) {
      $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
	  $user_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
      $user_password = mysqli_real_escape_string($dbc, trim($_POST['password']));

      if (!empty($user_username) && !empty($user_password)) {
        $query = "SELECT user_id, username FROM test_user WHERE username = '$user_username' AND password = SHA('$user_password')";
        $data = mysqli_query($dbc, $query);

        if (mysqli_num_rows($data) == 1) {
          $row = mysqli_fetch_array($data);
          $_SESSION['user_id'] = $row['user_id'];
          $_SESSION['username'] = $row['username'];
          setcookie('user_id', $row['user_id'], time() + (60 * 60 * 24 * 30));    // expires in 30 days
          setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30));  // expires in 30 days
          $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . 'index.php';
          header('Location: ' . $home_url);
        }
        else {
	
        $error_msg = '기입 정보를 확인하세요.';
        
	}
      }
      else {
        $error_msg = '기입 정보를 확인하세요.';
      }
    }
  }

  if (empty($_SESSION['user_id'])) {
   echo $error_msg;
?>
<!--
<h1>로그인</h1>
<form method="post" action="<? //echo $_SERVER['PHP_SELF'];?>">
	ID:<input type="text" name="username" />
    PW:<input type="password" name="password" />
	<input type="submit" name="login" value="로그인" />
</form>
--!>
<?
  }
  else {
    //echo('<p>Hello,' . $_SESSION['username'] . '.</p>');
//	echo ('<a href="logout.php">LOG OUT</a>');
  }
 
}

?>

